import os,sys,subprocess,urllib.request,json

if len(sys.argv) >1:
    os.chdir(sys.argv[1])

def toDel(s):
    #获取服务器地址
    config=open('.lfsconfig','r',encoding='utf-8')
    config=config.readlines()
    config=config[1]
    config=config.split('"')[1]
    headers = {'Content-Type': 'application/vnd.git-lfs+json','Accept': 'application/vnd.git-lfs+json'}
    data={"operation":"delete","objects":[]}
    for x in s:
        data["objects"].append({"oid":x})
    request = urllib.request.Request(config+"objects/batch", data=json.dumps(data).encode(), headers=headers, method='POST')
    urllib.request.urlopen(request)

a=subprocess.Popen("git lfs prune -d -v",env={"GIT_LFS_FORCE_PROGRESS":"1"},stdout=subprocess.PIPE,text=True)
a=a.stdout.readlines()
for (k,v) in enumerate(a):
    if v.startswith("prune"):
        continue
    break
a=a[k+1:-1]
s=set()
for x in a:
    s.add(x[3:67])
toDel(s)
print(f'服务器已删除 {len(s)} 个文件')